import {getSchoolInfo,UpdataSchoolInfoEdit} from "../../../api/Infomange"
import {reDeleteImage} from "../../../api/moretap"
import {onMounted,reactive,ref} from "vue"
import {useStore} from "vuex"
import {onShow,onLoad} from "@dcloudio/uni-app";
export function usegetSchoolInfo(){
	const {state, dispatch, commit} = useStore()
	const DAT = ref<any>([])
	const COUNT = ref<number>(0)
	const BASE_URL = import.meta.env.VITE_BASE_API
	function initgetSchoolInfo(){
		return new Promise((resolve,reject)=>{
			getSchoolInfo({
				OPERATOR_ID: state.user.userInfo.CUSER_ID
			})
			.then(res=>{
				if (res.ERROR[0].errcode == '0'){
					DAT.value = res.DAT
					COUNT.value = parseInt(String(res.COUNT[0].NUM / 10)) + (res.COUNT[0].NUM % 10 === 0 ? 0 : 1)
				    resolve(res)
				}else {
				    uni.showToast({
				        title: res.ERROR[0].error,
				        icon: 'none',
				        mask: true
				    })
				    reject('err')
				}
			})
			.catch(err=>{
				uni.showToast({
				    title: '获取列表失败',
				    icon: 'error',
				    mask: true
				})
				reject('err')
			})
		})
	}
	function initUploadimage(params:any){
		return new Promise((resolve,reject)=>{
			uni.showLoading({
				title: '上传图片中'
			});
			let filenames = ref<any>("")  //上传的图片名
			let Oldfilenames:string = ""
			for(let i =0;i<params.Oldimgs.length;i++){  //拼接旧图片
				if(i === params.Oldimgs.length-1){
					Oldfilenames = Oldfilenames + params.Oldimgs[i]
				}else{
					Oldfilenames = Oldfilenames + params.Oldimgs[i] + ";"
				}	
			}
			function setDetail(){
				let XX_PIC:string = ""
				if(Oldfilenames != ""){
					XX_PIC = Oldfilenames + ";"  + filenames.value
				}else{
					XX_PIC = filenames.value
				}
				UpdataSchoolInfoEdit({
					XX_ID:params.XX_ID,
					XX_BT:params.XX_BT,
					XX_TXT:params.XX_TXT,
					STATE :params.STATE,
					XX_PIC:XX_PIC,
					OPERATOR_ID:state.user.userInfo.CUSER_ID
				})
				.then(res=>{
					if (res.ERROR[0].errcode == '0'){
						if(params.Deteimages.length){
							for(let i = 0;i<params.Deteimages.length;i++){
								if(i === params.Deteimages.length-1){
									reDeleteImage({
										picfilename:params.Deteimages[i]
									})
									.then(res=>{
										// '删除之前照片'
										uni.showToast({
											title: params.XX_ID==0?"新增成功":"修改成功",
											icon: 'none',
											mask: true
										})
										setTimeout(()=>{
											uni.navigateBack({
												delta:1
											})
										},500)
									})
								}else{
									reDeleteImage({
										picfilename:params.Deteimages[i]
									})
								}
							}
							
						}else{
							uni.showToast({
								title: params.XX_ID==0?"新增成功":"修改成功",
								icon: 'none',
								mask: true
							})
							setTimeout(()=>{
								uni.navigateBack({
									delta:1
								})
							},500)
						}
						uni.hideLoading();
						commit("SET_SCHOOLINFO",true)
						resolve(res)
					}else {
						uni.showToast({
							title: res.ERROR[0].error,
							icon: 'none',
							mask: true
						})
						reject('err')
					}
				})
				.catch(err=>{
					reDeleteImage({
						picfilename:filenames.value
					})
					.then(res=>{
					// 修改失败删除当前上传照片
					})
				})
			}
			function uploadfile(Newimgs:any,index:number){
				uni.uploadFile({
					url: BASE_URL + '/pic_server/upload', //服务器 url
					filePath: params.Newimgs[index], // 要上传文件资源的路径
					name: 'file', // 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
					formData:{
						token:uni.getStorageSync("token"),
						CUSER_ID:state.user.userInfo.CUSER_ID,
						FLAG:"SYS",
						file:params.Newimgs[index]
					},
					success(res) {
						let tmpData = JSON.parse(res.data)
						if(index == params.Newimgs.length-1){
							filenames.value =  filenames.value + tmpData.DATA.filename
							setDetail()
						}else{
							filenames.value = filenames.value + tmpData.DATA.filename + ";"
							uploadfile(Newimgs,index+1)
						}
					},
					fail(err) {
					},
					complete() {
					},
				})
			}
			if(params.Newimgs.length != 0){
				uploadfile(params.Newimgs,0)
				
			}else{
				UpdataSchoolInfoEdit({
					XX_ID:params.XX_ID,
					XX_BT:params.XX_BT,
					XX_TXT:params.XX_TXT,
					STATE :params.STATE,
					XX_PIC:Oldfilenames,
					OPERATOR_ID:state.user.userInfo.CUSER_ID
				})
				.then(res=>{
					if (res.ERROR[0].errcode == '0'){
						if(params.Deteimages.length){
							for(let i = 0;i<params.Deteimages.length;i++){
								if(i === params.Deteimages.length-1){
									reDeleteImage({
										picfilename:params.Deteimages[i]
									})
									.then(res=>{
										// 删除之前照片
									})
								}else{
									reDeleteImage({
										picfilename:params.Deteimages[i]
									})
								}
							}
						}
						uni.showToast({
							title: "修改成功",
							icon: 'none',
							mask: true
						})
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},500)
						commit("SET_SCHOOLINFO",true)
						resolve(res)
					}else {
						uni.showToast({
							title: res.ERROR[0].error,
							icon: 'none',
							mask: true
						})
						reject('err')
					}
				})
				.catch(err=>{
					uni.showToast({
						title: '修改失败',
						icon: 'error',
						mask: true
					})
					reject('err')
				})
			}
			
		})
	}
		return {
		initgetSchoolInfo,
		DAT,
		COUNT,
		BASE_URL,
		initUploadimage
	}
}